//
//  AuthorViewController.m
//  EverydayDemo
//
//  Created by qianfeng01 on 15-7-17.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "AuthorViewController.h"
#import "AuthorModel.h"
#import "AuthorCell.h"
#import "UIImageView+WebCache.h"
#import "AuthorDetailController.h"
//#import "AuthorDetailViewController.h"
@interface AuthorViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIView *allView;
@property(nonatomic,strong)NSMutableArray *scrollDataArr;
@end

@implementation AuthorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr=[[NSMutableArray alloc]init];
    self.scrollDataArr=[[NSMutableArray alloc]init];
  //  [self creatView];
    [self creatCollectionView];
    [self creatLabel];
    [self downloadDataWithUrl:AuthorScrollViewUrl];
    [self downloadDataWithUrl:AuthorListUrl];

    
    
  

}
//-(void)creatView
//{
//    self.allView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenSize.width, (kScreenSize.height/3)+40)];
//    
//    
//}
-(void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // 对于垂直滚动，上下cell的间距，对于水平滚动，左右cell的间距 最小间距
    flowLayout.minimumLineSpacing = 10;
    
    // 设置item之间间距，实际间隔需要根据item的size和edgesInsets的值来调节
    flowLayout.minimumInteritemSpacing =0;
    
    // 全局设置itemSize
    flowLayout.itemSize = CGSizeMake(4*kScreenSize.width/9,179);
    
    // 内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,(kScreenSize.height/3)+40, kScreenSize.width,2*kScreenSize.height/3-89) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //_collectionView.showsVerticalScrollIndicator=NO;
    self.collectionView.backgroundColor=[UIColor colorWithRed:235/255 green:235/255 blue:235/255 alpha:0.1];
    // self.collectionView.backgroundColor=[UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"AuthorCell" bundle:nil] forCellWithReuseIdentifier:@"AuthorCell"];
    [self.view addSubview:_collectionView];
}
-(void)creatLabel
{
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(20, (kScreenSize.height/3)+10, kScreenSize.width, 30)];
    self.label.text=[NSString stringWithFormat:@"  全部作者"];
    [self.view addSubview:self.label];
}
-(void)creatScrollView
{
    self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height/3)];
    _scrollView.showsHorizontalScrollIndicator=NO;
    for (int i=0; i<self.scrollDataArr.count; i++) {
        AuthorModel *model=self.scrollDataArr[i];
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i* kScreenSize.width,0,kScreenSize.width, kScreenSize.height/3)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.image]placeholderImage:[UIImage imageNamed:@"default.png"]];
        [_scrollView addSubview:imageView];
        UIImageView *authorImageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*kScreenSize.width+30, (kScreenSize.height/3)-50, 30,30)];
      
        [authorImageView sd_setImageWithURL:[NSURL URLWithString:model.avator]];
        authorImageView.layer.masksToBounds=YES;
          authorImageView.layer.cornerRadius=15;
        [_scrollView addSubview:authorImageView];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(i*kScreenSize.width+80, (kScreenSize.height/3)-60, 80, 30)];
        label.text=model.name;
        label.textColor=[UIColor whiteColor];
        [_scrollView addSubview:label];
        
        UILabel *categoryLabel=[[UILabel alloc]initWithFrame:CGRectMake(i*kScreenSize.width+180,(kScreenSize.height/3)-60,200, 30)];
        categoryLabel.text=model.categoryStr;
        categoryLabel.textColor=[UIColor yellowColor];
        categoryLabel.font=[UIFont boldSystemFontOfSize:13.0];
        [_scrollView addSubview:categoryLabel];
        UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(i*kScreenSize.width+80, (kScreenSize.height/3)-40, 300, 30)];
        titleLabel.font=[UIFont boldSystemFontOfSize:13.0];
        titleLabel.text=model.title;
        titleLabel.textColor=[UIColor whiteColor];
        [_scrollView addSubview:titleLabel];
        
        UILabel *countLabel=[[UILabel alloc]initWithFrame:CGRectMake((i+1)*kScreenSize.width-40, (kScreenSize.height/3)-40, 40, 30)];
        NSString *str=[NSString stringWithFormat:@"%d/%ld",i+1,(unsigned long)self.scrollDataArr.count];
        countLabel.textColor=[UIColor redColor];
        countLabel.text=str;
        [_scrollView addSubview:countLabel];
                                                                                  
    }
    UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(tap:)];
    [_scrollView addGestureRecognizer:tap];
    _scrollView.pagingEnabled=YES;
    _scrollView.contentSize=CGSizeMake(self.scrollDataArr.count*kScreenSize.width, kScreenSize.height/3);
    [self.view addSubview:_scrollView];
}
-(void)tap:(UITapGestureRecognizer *)tap
{
    AuthorDetailController *cnv=[[AuthorDetailController alloc]init];
    UIScrollView *view=(UIScrollView *)tap.view;
    int a=view.contentOffset.x/(kScreenSize.width);
    AuthorModel *model=self.scrollDataArr[a];
    cnv.authorId=model.authorId;
    cnv.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:cnv animated:YES];
}
-(void)downloadDataWithUrl:(NSString *)url
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    //只下载  不解析
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof (self) weakSelf=self;
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *array=dict[@"result"];
            //[weakSelf.dataArr removeAllObjects];
            for (NSDictionary *dedict in array) {
                AuthorModel *model=[[AuthorModel alloc]init];
                [model setValue:dedict];
                if ([url isEqualToString:AuthorScrollViewUrl]) {
                    [weakSelf.scrollDataArr addObject:model];
                }else{
                [weakSelf.dataArr addObject:model];
                }
            }
        }
        if ([url isEqualToString:AuthorScrollViewUrl]) {
              [weakSelf creatScrollView];
        }
        if ([url isEqualToString:AuthorListUrl]) {
            [weakSelf.collectionView reloadData];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载数据错误");
    }];

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AuthorCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"AuthorCell" forIndexPath:indexPath];
    AuthorModel *model=self.dataArr[indexPath.row];
    [cell setData:model];
   return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AuthorDetailController *con=[[AuthorDetailController alloc]init];
    AuthorModel *model=self.dataArr[indexPath.row];
    con.authorId=model.authorId;
    con.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:con animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
